export default {
  data() {
    return {
      //
      formData: {},
      dataList: []
      // query: {}
    }
  },
  computed: {
    // dataList() {
    //   return this.$store.state[this.storeModel].dataList
    // },
    dataDict() {
      return this.$store.state[this.storeModel].dataDict
    }
  },

  watch: {},

  async created() {
    console.log(' Mixin ', this.$route.path)

    const path = this.$route.path
    const ss = path.substring(path.length - 4)

    const fns = {
      list: this.init_list,
      view: this.init_view,
      form: this.init_form
    }

    const fn = fns[ss]

    if (fn) {
      fn()
    }
  },

  methods: {
    //
    async init_list() {
      // console.log(' Mixin init_list')
      // header right
      this.$store.dispatch('header/setRight', {
        path: this.config.path.form,
        icon: 'plus'
        // fn: () => {
        //   console.log('test fn')
        // }
      })

      // data
      const domain = this.config.domain || {}
      const action = `${this.storeModel}/search`
      await this.$store.dispatch(action, { domain })
    },

    async list_load_data({ limit, offset }) {
      //
      // data
      const domain = this.config.domain || {}
      const order = this.config.order || ''
      const action = `${this.storeModel}/search`
      const res = await this.$store.dispatch(action, {
        domain,
        order,
        limit,
        offset
      })
      this.dataList = [...this.dataList, ...res]
      // const data = this.$store.state[this.storeModel].dataList
      // console.log('Mixin res,', data)
      return res
    },

    async init_view() {
      const { id } = this.$route.query
      const rid = Number(id)

      this.$store.dispatch('header/setRight', {
        path: this.config.path.form,
        query: { id: rid },
        icon: 'edit'
      })

      const action = `${this.storeModel}/browse_one`
      await this.$store.dispatch(action, { id: rid })
      const data = this.$store.state[this.storeModel].dataDict
      console.log('res,', data)
    },

    async init_form() {
      const { id } = this.$route.query
      const title = id ? this.config.titleWrite : this.config.titleCreate
      const title2 = title || (id ? '编辑' : '创建')
      await this.$store.dispatch('header/setTitle', title2)

      if (id) {
        const rid = Number(id)
        const action = `${this.storeModel}/browse_one`
        await this.$store.dispatch(action, { id: rid })

        const formData = this.$store.state[this.storeModel].formData
        this.formData = { ...formData }
      }
    },

    async onSubmit(values) {
      //
      console.log('onSubmit', values)
      const { id } = values
      if (id) {
        const action = `${this.storeModel}/write`
        await this.$store.dispatch(action, { values })
        this.$router.back()
      } else {
        const action = `${this.storeModel}/create`
        const res = await this.$store.dispatch(action, { values })
        this.$router.replace({
          path: this.config.path.view,
          query: { id: res.id }
        })
      }
    },

    async onDelete(values, cb) {
      const { id } = values
      if (id) {
        const action = `${this.storeModel}/unlink`
        await this.$store.dispatch(action, { id })
      }
      if (cb) {
        cb()
      }
      this.$router.back()
      this.$router.back()
    }
  }
}
